<template>
    <el-dialog title="Training Activity Entry" :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="dataForm" ref="dataForm" label-width="100px">
            <el-form-item label="trainingName">
                <el-input v-model="dataForm.trainingName" placeholder="Fill in the training trainingName"></el-input>
            </el-form-item>
            <el-form-item label="trainingLevel">
                <el-input v-model="dataForm.trainingLevel" placeholder="Fill in the training trainingLevel"></el-input>
            </el-form-item>
            <el-form-item label="trainingAddress">
                <el-input v-model="dataForm.trainingAddress" placeholder="Fill in the training trainingAddress"></el-input>
            </el-form-item>
            <el-form-item label="trainingStartDate">
                <el-date-picker
                v-model="dataForm.trainingStartDate"
                type="datetime"
                value-format="timestamp"
                placeholder="Fill in the training trainingStartDate">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="trainingEndDate">
                <el-date-picker
                v-model="dataForm.trainingEndDate"
                type="datetime"
                value-format="timestamp"
                placeholder="Fill in the training trainingEndDate">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="comment">
                <el-input type="textarea" v-model="dataForm.comment" placeholder="Fill in the training comment"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">submit</el-button>
                <el-button @click="visible = false">cancel</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          trainingName: '',
          trainingLevel: '',
          trainingAddress: '',
          trainingStartDate: '',
          trainingEndDate: '',
          comment: ''
        }
      }
    },
    methods: {
      init (id) {
        console.log(id)
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/edu/tedutrain/queryTrainById/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.id = data.data.id
              this.dataForm.trainingName = data.data.trainingName
              this.dataForm.trainingLevel = data.data.trainingLevel
              this.dataForm.trainingAddress = data.data.trainingAddress
              this.dataForm.trainingStartDate = data.data.trainingStartDate
              this.dataForm.trainingEndDate = data.data.trainingEndDate
              this.dataForm.comment = data.data.comment
            }
          })
        } else {
          this['dataForm'] = {}
        }
      },
      onSubmit () {
        console.log(this.dataForm)
        this.$http({
          url: this.$http.adornUrl(`/edu/tedutrain/`),
          method: !this.dataForm.id ? 'post' : 'put',
          data: this.dataForm
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: 'Operation succeeded',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>